<?php
// +----------------------------------------------------------------------
// | title: 玩家留存统计
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2020/1/7 14:13
// +----------------------------------------------------------------------

namespace app\single\command;


use app\single\model\PlayerOnlineModel;
use think\Command;
use think\Db;

class PlayerKeep extends Command
{
    protected function execute($input, $output)
    {
        $playerOnlineModel = new PlayerOnlineModel();
        $keepDays = range(2, 90);
        $day = date("Ymd", strtotime($this->sDate));
        $eDate = date("Ymd", strtotime($this->eDate));
        // print_r("$day,$eDate");
        while ($day <= $eDate) {
            foreach ($keepDays as $keep) {
                //玩家注册时间 (keep - 1) * 86400
                $regDay = date("Ymd", strtotime($day) - ($keep - 1) * 86400);
                $data = $playerOnlineModel->playerKeep($day, $regDay);
//                var_dump($data);
                foreach ($data as $type => $arr) {
                    foreach ($arr as $platform => $num) {
                        $check = Db::table("c_player_keep")
                            ->where(["day" => $regDay, "type" => $type, "platform" => $platform, "keep_day" => $keep])
                            ->value("id");
                        if ($check) {
                            Db::table("c_player_keep")
                                ->where("id", $check)
                                ->update([
                                    "player_num" => $num,
                                ]);
                        } else {
                            Db::table("c_player_keep")
                                ->insert([
                                    "day" => $regDay,
                                    "type" => $type,
                                    "platform" => $platform,
                                    "keep_day" => $keep,
                                    "player_num" => $num
                                ]);
                        }
                    }
                }
            }
            //日期+1
            $day = date('Ymd', strtotime($day) + 86400);
        }
    }
}